package cn.gzsxt.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSON;

import cn.gzsxt.vo.MsgObj;

@WebServlet("/FileUpload.oa")
public class FileUploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		MsgObj msgObj = new MsgObj(0,"亲,您上传的单个文件过大!");
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		if (isMultipart) {
			try {
				DiskFileItemFactory factory = new DiskFileItemFactory();
				factory.setSizeThreshold(500*1024);
				factory.setRepository(new File("c:/temp"));
				ServletFileUpload upload = new ServletFileUpload(factory);
				
				upload.setFileSizeMax(500 * 1024);
				upload.setSizeMax(500 * 1024);
				
				List<FileItem> items = upload.parseRequest(req);
				System.out.println(items);
				
				for (FileItem fileItem : items) {
					String fieldName = fileItem.getFieldName();
					if(!fileItem.isFormField()){
						String fileName = fileItem.getName();
						System.out.println(fileName);
						String uuid = UUID.randomUUID().toString().replaceAll("-", "");
						System.out.println(uuid);
						String extension = FilenameUtils.getExtension(fileName);
						String newFileName = uuid+"."+extension;
						
						String contentType = fileItem.getContentType();
						InputStream inputStream = fileItem.getInputStream();
						String realPath = req.getServletContext().getRealPath("/upload")+"/";
						System.out.println("realPath:"+realPath);
						File targetFile = new File(realPath);
						if(!targetFile.exists()){
							targetFile.mkdir();
						}
						
						OutputStream outputStream = new FileOutputStream(new File(targetFile,newFileName));
						IOUtils.copy(inputStream, outputStream);
						msgObj = new MsgObj(1,"/upload/"+newFileName);
						resp.getWriter().print(JSON.toJSONString(msgObj));
					}
				}
			} catch(FileSizeLimitExceededException e){
				resp.getWriter().print(JSON.toJSONString(msgObj));
				
			}catch (Exception e) {
				msgObj = new MsgObj(0,"服务器异常,稍后再试!!!");
				resp.getWriter().print(JSON.toJSONString(msgObj));
				System.out.println("异常");
				e.printStackTrace();
			}
			
		}
		
	}
}
		
